//
//  HDOperationActivitiesData.m
//  HDOperationProject
//
//  Created by Mirko on 15/10/30.
//  Copyright (c) 2015年 Mirko. All rights reserved.
//

#import "HDOperationActivitiesData.h"
#import "HDNetworkOperationManager.h"
#import "HDOperationTools.h"

typedef NS_ENUM(NSInteger, kEOperationTimeType) {
    
    kEOperationTimeTypeDefault,         // 默认类型
    kEOperationTimeTypeTime,            // 展示次数
    kEOperationTimeTypeInterval,        // 区间段类型
};

@interface HDOperationActivitiesData ()

@property (nonatomic, strong, readwrite) NSString * operationId;       // 活动ID
@property (nonatomic, assign, readwrite) NSTimeInterval displayTime;   // 活动页面展示时间

@property (nonatomic, assign, readwrite) BOOL allowClose;              // 活动页面是否允许关闭
@property (nonatomic, strong, readwrite) NSString *operationUrl;       // 活动页面展示图片
@property (nonatomic, strong, readwrite) NSString *linkUrl;            // 活动点击展示的URL链接

@property (nonatomic, assign) NSInteger operationTimeType;              // 活动展示类型  区间段  展示次数

@property (nonatomic, assign) NSTimeInterval startTime;                 // 活动开始时间  区间段
@property (nonatomic, assign) NSTimeInterval endTime;                   // 活动结束时间  区间段

@property (nonatomic, assign) NSInteger displayCount;                   // 非区间段 展示次数
//@property (nonatomic, assign, readwrite) BOOL isChange;                 // 推广活动是否有变化
@property (nonatomic, assign) NSInteger status;                         // 上线状态（1:未上线， 2:上线）

//////////////
@property (nonatomic, assign, readwrite) BOOL isNeedDisplay;            // 活动图片是否需要删除
@property (nonatomic, assign, readwrite) BOOL isNeedDelete;             // 活动图片是否需要删除

@end

@implementation HDOperationActivitiesData

+ (HDOperationActivitiesData *)testModel {
    
    HDOperationActivitiesData *model = [[HDOperationActivitiesData alloc] init];
    
    model.allowClose = YES;
    model.isNeedDisplay = YES;
    
    return model;
}

/** 把普通数组转换为model数组 */
+ (NSArray *)modelsFromArray:(NSArray *)arr {
    
    NSMutableArray *mutaArr = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        [mutaArr addObject:[HDOperationActivitiesData data:dic]];
    }
    return [NSArray arrayWithArray:mutaArr];
}

+(HDOperationActivitiesData *) data:(NSDictionary *) dataInfo {
    HDOperationActivitiesData *activities = [[HDOperationActivitiesData alloc] initWithInfo:dataInfo];
    return activities;
}

/** 获取一个默认的广告信息 */
+(HDOperationActivitiesData *)defaultAdInfo {
    
    HDOperationActivitiesData *activities = [[HDOperationActivitiesData alloc] init];
    
    activities.allowClose = YES;
    activities.displayTime = 3;
    
    activities.fileName = @"default_ad.png";
    activities.pathDisk = [[NSBundle mainBundle] pathForResource:@"default_ad.png" ofType:nil];
    
    return activities;
}

-(instancetype) initWithInfo:(NSDictionary *) dataInfo
{
    self = [super init];
    if (self) {
        
        self.allowClose         = [dataInfo[@"allow_close"] boolValue];
        
        //TODO:
//        self.allowClose = YES;
        
        self.displayTime        = [dataInfo[@"display_time"] floatValue];
        
//        self.displayTime = 0;
        
        self.endTime            = [dataInfo[@"end_time"] doubleValue];
        self.startTime          = [dataInfo[@"start_time"] doubleValue];
        
        self.operationTimeType  = [dataInfo[@"type"] integerValue];
        self.operationUrl       = dataInfo[@"img"];
        self.linkUrl            = dataInfo[@"url"];
        
        self.status             = [dataInfo[@"status"] integerValue];
        
//        // 推广活动是否有变化
//        self.isChange           = [dataInfo[@"isChange"] boolValue];
//        
//        self.isChange = YES;
        
        /////////////
        self.displayCount       = [dataInfo[@"displayCount"] integerValue];
        self.operationId        = dataInfo[@"operationId"];
    }
    return self;
}

- (NSString *)description {
    
    return [NSString stringWithFormat:@"allowClose: %d \n displayTime: %lf \n operationTimeType: %ld \n operationUrl: %@ \n linkUrl: %@ \n displayCount: %ld \n operationId: %@", self.allowClose, self.displayTime, (long)self.operationTimeType, self.operationUrl, self.linkUrl, (long)self.displayCount, self.operationId];
}

// 活动图片
- (UIImage *)image {
    
    UIImage *image = [UIImage imageWithContentsOfFile:self.pathDisk];
    
    NSLog(@"图片路径: %@", self.pathDisk);
    return image;
}

// 本地文件名字
- (NSString *)fileName {
    
    if (_fileName == nil && _operationUrl) {
        _fileName = [NSURL URLWithString:_operationUrl].lastPathComponent;
    }
    
    return _fileName;
}

// 文件绝对路径
- (NSString *)pathDisk {
    
    if (!_pathDisk) {
        
        
        
        _pathDisk = [[HDOperationTools getAdImageFolderPath] stringByAppendingPathComponent:self.fileName];
    }
    return _pathDisk;
}

// 是否需要显示该广告图片(如果当前时间，在广告图片上线时间区域内，就显示该图片)
- (BOOL)isNeedDisplay {
    
    // 获取当前时间
    NSTimeInterval nowTimeInterval = [HDOperationTools getGMTTimeZoneTimeInterval];
    
    NSLog(@"判断是否显示广告图片，北京时间: %lf 开始时间: %lf 结束时间: %lf", nowTimeInterval, self.startTime, self.endTime);
    
    if (nowTimeInterval >= self.startTime && nowTimeInterval <= self.endTime) {
        
        return YES;
    }
    
    return NO;
}

// 是否需要删除该广告图片(如果当前时间已经超过上线时间的结束时间)
- (BOOL)isNeedDelete {
    
    // 获取当前时间
    NSTimeInterval nowTimeInterval = [HDOperationTools getGMTTimeZoneTimeInterval];
    
    NSLog(@"判断是否删除广告图片,北京时间: %lf 开始时间: %lf 结束时间: %lf", nowTimeInterval, self.startTime, self.endTime);
    
    if (nowTimeInterval > self.endTime) {
        
        return YES;
    }
    
    return NO;
}

// 下载活动图片
- (void)startDownloadImage {
    
    [HDNetworkOperationManager downLoadImageWithURL:self.operationUrl
                                  completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
                                     
                                      if (error == nil) {
                                          
                                          NSString *locationPath = [NSString stringWithFormat:@"%s", location.fileSystemRepresentation];
                                          NSLog(@"下载图片的路径...%@", locationPath);
                                          
                                          // 把下载到临时文件目录中的图片保存到，图片目录中
                                          [HDOperationTools copyImageToAdimageFoldWithImagePath:locationPath newImageName:self.fileName];
                                          
                                      }else {
                                          
                                      }
                                  }];
}

@end
    